package net.xo68.destiny.manager.config;

import net.xo68.destiny.core.json.JsonMapper;
import net.xo68.destiny.manager.security.EntryPointUnauthorizedHandler;
import net.xo68.destiny.manager.security.RestAccessDeniedHandler;
import net.xo68.destiny.manager.security.UserJwtTokenAuthenticationFilter;
import net.xo68.destiny.manager.security.UserJwtTokenTemplate;
import net.xo68.destiny.starter.common.SchedulerCommonProperties;
import net.xo68.destiny.starter.manager.SchedulerManagerProperties;
import net.xo68.destiny.starter.manager.security.ApiAuthorizationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

/**
 * web安全配置
 *  具体配置请参见spring security官方文档
 * @author wuxie
 * @version 2020-9-14
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SchedulerCommonProperties schedulerCommonProperties;
    @Autowired
    private SchedulerManagerProperties schedulerManagerProperties;
    @Autowired
    private JsonMapper jsonMapper;
    @Autowired
    private EntryPointUnauthorizedHandler entryPointUnauthorizedHandler;
    @Autowired
    private RestAccessDeniedHandler restAccessDeniedHandler;
    @Autowired
    private UserJwtTokenTemplate userJwtTokenTemplate;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //auth.userDetailsService(systemUserService);
        //auth.authenticationProvider(jwtAuthenticationProvider);
        super.configure(auth);
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                //.exceptionHandling().accessDeniedHandler(accessDeniedHandler).and()
                // 由于使用的是JWT，我们这里不需要csrf
                .csrf().disable()
                // .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 基于token，所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers("/api/**").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(entryPointUnauthorizedHandler)
                .accessDeniedHandler(restAccessDeniedHandler)
                .and()
                .addFilterBefore(new UserJwtTokenAuthenticationFilter(authenticationManager(),userJwtTokenTemplate,"/api/**"), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new ApiAuthorizationFilter(schedulerCommonProperties,jsonMapper,"/api/**"), UsernamePasswordAuthenticationFilter.class)
                // 禁用缓存
                .headers().cacheControl();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/webjars/**","/favicon.ico",
                "/login",
                "/error/**",
                "/doc.html","/swagger-ui.html","/swagger-resources/**","/*/api-docs"
        );
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.applyPermitDefaultValues();
        source.registerCorsConfiguration("/**", config);
        return source;
    }
}
